/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-31下午06:05:40
 */
package hit.edance.campusmobile.chat.model;

import hit.edance.campusmobile.chat.service.CampusMobileService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.packet.Presence;

import android.util.Log;

/**   
 * 本地的好友信息备份及信息处理<br/>
 * 单例<br/>
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.model
 * @Class FriendsManager
 * @Date 2012-5-31 下午06:05:40
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class FriendsManager implements Comparator{
	
	private static FriendsManager instance;
	private static List<User> friends;
	private static List<RosterGroup> groups;
	private static HashMap<String,List<User>> groupChildMap;
	
	private FriendsManager(){
		friends = new ArrayList<User>();
		groups = new ArrayList<RosterGroup>();
		groupChildMap = new HashMap<String,List<User>>();
	}
	public static FriendsManager getInstance() {
		if(instance == null)
			instance = new FriendsManager();
	      return instance;
	  }
	/**
	 * @return the friends
	 */
	public List<User> getFriends() {
		return friends;
	}
	/**
	 * @param friends the friends to set
	 */
	public void setFriends(Roster roster) {
		friends.clear();
		for (RosterEntry entry : roster.getEntries()) {
			Presence presence = roster.getPresence(entry.getUser());
			
			User user = new User();
			for(RosterGroup usergroup : entry.getGroups())
				user.addGroup(usergroup);
			user.setJabberId(entry.getUser()).setStatus(presence.getStatus());
			User.getUserInfo(CampusMobileService.connection, user);

			friends.add(user);
		}
	}
	/**
	 * @return the groups
	 */
	public List<RosterGroup> getGroups() {
		return groups;
	}
	/**
	 * @param groups the groups to set
	 */
	public void setGroups(Roster roster) {
		for(RosterGroup group : roster.getGroups()){
			groups.add(group);
		}
	}
	/**
	 * @return the groupChildMap
	 */
	public HashMap<String, List<User>> getGroupChildMap() {
		return groupChildMap;
	}
	/**
	 * @param groupChildMap the groupChildMap to set
	 */
	public void setGroupChildMap(Roster roster) {
		setGroups(roster);
		setFriends(roster);
		for(RosterGroup rostergroup : groups){
			groupChildMap.put(rostergroup.getName(), new ArrayList<User>());
			for(User user : friends){
				if(user.getGroups().contains(rostergroup))
					groupChildMap.get(rostergroup.getName()).add(user);
			}
		}
	}
	public void sortGroupChildMap(){
		for(RosterGroup rostergroup : groups){
			sortByStatus(groupChildMap.get(rostergroup.getName()));
		}
	}
	/**
	 * 按照状态进行排序
	 * @param users
	 */
	public void sortByStatus(List<User> users){
		Collections.sort(users, FriendsManager.getInstance());
	}
	/**
	 * 获取在线人数
	 * @param users
	 */
	public int getOnLineCountInList(List<User> users){
		int i = 0;
		for(User user : users){
			if(user.getStatus()== null || user.getStatus().equals(""))
				continue;
				i++;
		}
		return i;
	}
	/* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 *compare
	 */
	public int compare(Object o0, Object o1) {
		User user0 = (User)o0;
		User user1 = (User)o1;
//		Log.i("user0",user0.getJabberId()+"-"+user0.getStatus());
//		Log.i("user1",user1.getJabberId()+"-"+user1.getStatus());
//		Log.i("-----","------");
		int u0 = user0.getStatus() == null ? 0 : 1,
			u1 = user1.getStatus() == null ? 0 : 1;	
		return u1-u0;
	}
	public static void close(){
		groupChildMap.clear();
		groups.clear();
		friends.clear();
	}
	
}
